package com.whc.gulimall.scekill.schedules;

import com.whc.gulimall.scekill.service.SecKillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author angaoen
 */
@Slf4j
@Service
public class SecKillScheduled {

    @Resource
    private SecKillService secKillService;

    @Resource
    private RedissonClient redissonClient;

    private final String UPLOAD_SECKILL = "upload:seckill";

    /**
     * 定时任务，上架最近三天的秒杀商品信息
     */
//    @Scheduled(cron = "0/10 * * * * ?")
    public void upSecKillProduct() {
        RLock lock = redissonClient.getLock(UPLOAD_SECKILL);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            secKillService.upSecKillProductOfThreeDays();
            log.info("secKillService.upSecKillProductOfThreeDays()");
        } catch (ExecutionException | InterruptedException e) {
            log.error(e.getLocalizedMessage());
        } finally {
            lock.unlock();
        }
    }


}
